## Creating Routers in Neutron (Chapter 6)
## CONTROLLER NODE ONLY!


## Installation ##

#LinuxBridge Only
crudini --set /etc/neutron/l3_agent.ini DEFAULT interface_driver neutron.agent.linux.interface.BridgeInterfaceDriver
# End LinuxBridge Only

#Open vSwitch Only
crudini --set /etc/neutron/l3_agent.ini DEFAULT interface_driver neutron.agent.linux.interface.OVSInterfaceDriver
# End Open vSwitch Only

crudini --set /etc/neutron/l3_agent.ini DEFAULT external_network_bridge
service neutron-l3-agent start
chkconfig neutron-l3-agent on

## Demonstration ##
neutron net-create --provider:network_type=vlan --provider:segmentation_id=50 --provider:physical_network=physnet1 --router:external=true --shared GATEWAY_NET
neutron subnet-create GATEWAY_NET 10.50.0.0/24 --name GATEWAY_SUBNET --allocation-pool start=10.50.0.100,end=10.50.0.254 --gateway 10.50.0.1
neutron router-create MyRouter
neutron router-gateway-set MyRouter GATEWAY_NET

neutron net-create TENANT_NET1
neutron subnet-create TENANT_NET1 10.30.0.0/24 --name TENANT_SUBNET1 --dns-nameserver 8.8.8.8
neutron router-interface-add MyRouter TENANT_SUBNET1

# Boot Test Instances (single command)
for i in {1..2}; do nova boot --flavor m1.tiny --image $(nova image-list | grep Cirr | awk '{ print $2 }') \
--nic net-id=$(neutron net-list | grep TENANT | awk '{ print $2 }') MyInstance$i; done

# Confirm instances booted
nova list

# Add security group rules
for SECID in $(neutron security-group-list | grep default | awk '{print $2}'); \
do neutron security-group-rule-create --protocol icmp $SECID; \
neutron security-group-rule-create --protocol tcp --port-range-min 22 --port-range-max 22 $SECID; \
done;

# Test connectivity to instance from router namespace
ip netns exec $(ip netns | grep qrouter) ssh cirros@$(nova show MyInstance1 | grep TENANT_NET1 | awk '{print $5}')

# Create floating IP
neutron floatingip-create --port-id=$(neutron port-list | grep $(nova show MyInstance1 | grep TENANT_NET1 | awk '{print $5}') | awk '{ print $2}') GATEWAY_NET
